import { Component, OnInit } from '@angular/core';
import {Student, StudentService} from '../share/student.service';
import {Router} from '@angular/router';
import {FormControl} from '@angular/forms';
import 'rxjs/add/operator/debounceTime';

@Component({
  selector: 'app-student-manager',
  templateUrl: './student-manager.component.html',
  styleUrls: ['./student-manager.component.css']
})
export class StudentManagerComponent implements OnInit {
  private students: Student[];
  private filterController: FormControl = new FormControl();
  private keyword: string;

  constructor(private studentService: StudentService, private router: Router) {

  }

  ngOnInit() {
    this.students = this.studentService.getStudents();
    this.filterController.valueChanges
      .debounceTime(500)
      .subscribe(value => {
        this.keyword = value;
        // console.log(this.keyword);
        // console.log(this.filterController.value);
      });
  }

  create() {
    this.router.navigateByUrl('/studentForm/0');
  }

  update(student: Student) {
    // console.log(student);
    this.router.navigateByUrl('/studentForm/' + student.id);
  }

  delete(student: Student) {
    // console.log(student);
    alert('你确定要删除' + student.name + '同学的信息吗？');
  }

}
